<!DOCTYPE html><!--查看个人详细信息-->
<html lang="en" xmlns:th="http://www.thymeleaf.org" class="x-admin-sm">
<head th:replace="common/amazingHeader2::amazing_header('个人详情')">
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form" id="MyForm">
            <div class="layui-form-item">
                <label for="username" class="layui-form-label">
                    <span class="x-red">*</span>用户名
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="username" name="username" required="" lay-verify="required"
                           autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red" id="nameMsg"></span>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" for="sex"><span class="x-red">*</span>性别</label>
                <div class="layui-input-inline" style="width: 14%">
                    <select id="sex" class="layui-form-select" name="sex">
                        <option value="男" id="man">男</option>
                        <option value="女" id="woman">女</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="phone" class="layui-form-label">
                    <span class="x-red">*</span>手机
                </label>
                <div class="layui-input-inline">
                    <input type="tel" id="phone" name="phone" required="" lay-verify="phone"
                           autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red" id="phoneMsg"></span>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="email" class="layui-form-label">
                    <span class="x-red">*</span>邮箱
                </label>
                <div class="layui-input-inline">
                    <input type="email" id="email" name="email" required="" lay-verify="email"
                           autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red" id="emailMsg"></span>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" for="question"><span class="x-red">*</span>密保问题</label>
                <div class="layui-input-inline" style="width: 14%">
                    <select id="question" class="layui-form-select" name="question">
                        <option value="你的家乡在哪?" id="q1">你的家乡在哪?</option>
                        <option value="你的小学叫什么?" id="q2">你的小学叫什么?</option>
                        <option value="你的小名叫什么?" id="q3">你的小名叫什么?</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="answer" class="layui-form-label">
                    <span class="x-red">*</span>答案
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="answer" name="answer" required=""
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-inline">
                    <input type="hidden" id="id" name="id" required=""
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item" style="padding-left: 150px">
                <button class="layui-btn" type="button" lay-submit="" onclick="changeUser()">
                    修改
                </button>
            </div>
        </form>
    </div>
</div>
</body>
<script>
    $.ajax({
        type: 'GET',
        url: 'user/detail',
        success: function (result) {
            const user = result.data;
            $('#username').val(user.username);
            if (user.sex === '男') {
                $('#man').attr('selected', true);
            } else {
                $('#woman').attr('selected', true);
            }
            $('#phone').val(user.phone);
            $('#email').val(user.email);
            if (user.question.search('家乡') !== -1) {
                $('#q1').attr('selected', true);
            } else if (user.question.search('小学') !== -1) {
                $('#q2').attr('selected', true);
            } else {
                $('#q3').attr('selected', true);
            }
            $('#answer').val(user.answer);
            $('#id').val(user.id);
        }
    });

    function changeUser() {
        if (!testUsername()) {
            return false;
        }
        if (!testAnswer()) {
            return false;
        }
        const formData = new FormData($('#MyForm')[0]);
        $.ajax({
            type: 'POST',
            url: 'user/changeUser',
            data: formData,
            cache: false,
            processData : false,  //必须false才会避开jQuery对 formdata 的默认处理
            contentType : false,  //必须false才会自动加上正确的Content-Type
            success: function () {
                layer.msg('修改成功', {time: 1000, icon: 6, shift: 6});
                setTimeout(xadmin.close, 1000);
            }
        })
    }
</script>
<script>
    layui.use(['form', 'layer'],
        function () {
            let form = layui.form,
                layer = layui.layer;

        });
</script>
</html>